import WxValidate from '../../../utils/WxValidate.js'

const app = getApp()

let Validate = ""

Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      name: '',
      mobile: '',
      address: '',
      remark: '',
      history: ''
    },
    mobile: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    const rules = {
      name: {
        required: true
      },
      account: {
        required: true,
      },
      code: {
        required: true
      },
      branch: {
        required: true
      }
    }

    const messages = {
      name: {
        required: '请输入姓名',
      },
      account: {
        required: '请输入手机号码',
      },
      code: {
        required: '请输入地址',
      },
      branch: {
        required: '请输入地址',
      }
    }
    Validate = new WxValidate(rules, messages)
    console.log(options)
    this.setData({
      options: !options.title ? false : JSON.parse(options.title),
    })
  },
  chooseicon: function (e) {
    var strnumber = e.target.dataset.id;
    var _obj = {};
    _obj.curHdIndex = strnumber;
    this.setData({
      tabArr: _obj
    });

  },
  editBank(e, options){
    app.query('/api/users/update_bank_card', { id: options.id, bank_card: e.detail.value }, 'post').then(res => {
      // console.log(res)
      if (res.status == 'success') {
        app.showToast('更新成功', 'success', 2000)
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 3000)


      }
    });
  },
  postBank(e){
    app.query('/api/users/add_bank_card', { bank_card: e.detail.value }, 'post').then(res => {
      // console.log(res)
      if (res.status == 'success') {
        app.showToast('添加成功', 'success', 2000)
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 3000)


      }
    });
  },
  formSubmit: function (e) {
    const params = e.detail.value 
    const {options} = this.data
    let _reg = /^(\d{16}|\d{19})$/;

    if (!Validate.checkForm(e)) {
      const error = Validate.errorList[0]
      app.showToast(error.msg, false, 500, '../../../assets/warning.png')
      //提示信息  
      // console.log(error, e)
      return false
    }

    if (!_reg.test(params.code.trim().replace(/\s/g, ""))){
      app.showToast('银行卡格式错误', false, 500, '../../../assets/warning.png')
      return
    }
    e.detail.value.code = e.detail.value.code.trim().replace(/\s/g, "")
    // console.log(!options.id)
    // return

    if (!options.id){
      this.postBank(e)
    }else{
      this.editBank(e, options)
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})